package com.shanghai.shanghaielectric.UI;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shanghai.shanghaielectric.R;
import com.shanghai.shanghaielectric.utils.CapacitanceStateView;

import java.util.ArrayList;
import java.util.List;


/**
 * 监测界面 Author: liudanhua Date: 15-11-23 Time: 下午2:39
 * <p>
 * MonitorFragment extends Fragment现在改为  MonitorFragment extends TabFragment
 */
public class MonitorFragment extends Fragment {

    private CapacitanceStateView mStateView;
//    private TextView mVoltagePhaseSequenceTv;
    private TextView mActiveTv;
    private TextView mRectiveTv;
    private TextView mFactorTv;
//    private ABCValueView mAbcView;

    private RadioGroup monitorrgs;
    public List<Fragment> monitorfragments = new ArrayList<Fragment>();
    public View monitorView;
    private int pause = 0;
    private RadioButton radio_monitor_basic;
    private MonitorBasicFragment monitorBasic;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        System.out.println("Monitor____onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("Monitor____onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        System.out.println("Monitor____onCreateView");
        pause = 0;
        monitorView = inflater.inflate(R.layout.monitor_fragment, container,
                false);

        radio_monitor_basic = (RadioButton) monitorView.findViewById(R.id.radio_monitor_basic);
        monitorBasic = new MonitorBasicFragment();

        monitorfragments.add(monitorBasic);
        monitorfragments.add(new MonitorUnitFragment());

        monitorrgs = (RadioGroup) monitorView
                .findViewById(R.id.tabs_monitor_rg);

        FragmentTabAdapter_Main tabAdapter = new FragmentTabAdapter_Main(
                /*   原来是  this.getActivity(), monitorfragments, R.id.tab_monitor_content,
                monitorrgs);  现在自动修复为下面的代码：*/
                (FragmentActivity) this.getActivity(), monitorfragments, R.id.tab_monitor_content,
                monitorrgs);
        tabAdapter
                .setOnRgsExtraCheckedChangedListener(new FragmentTabAdapter_Main.OnRgsExtraCheckedChangedListener() {
                    @Override
                    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup,
                                                         int checkedId, int index) {
                        System.out.println("Extra---- " + index
                                + " checked!!! ");
                    }
                });

        return monitorView;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("Monitor____onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("Monitor____onStart");
    }


    @Override
    public void onResume() {
        super.onResume();
        if (pause == 1) {
            pause = 0;
            radio_monitor_basic.setChecked(true);
            if (monitorBasic.isAdded()) {
                monitorBasic.onResume(); // 启动目标tab的onResume()
            }
        }
        System.out.println("Monitor____onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        pause = 1;
        if (monitorBasic.isAdded()) {
            monitorBasic.onPause(); // 启动目标tab的onResume()
        }
        System.out.println("Monitor____onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("Monitor____onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("Monitor____onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("Monitor____onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("Monitor____onDetach");
    }


/*    //此处是修改继承的类，新实现的方法   主要功能界面的实现！！！！！
    // 问题：这里不能刷新界面！！！原因：应该是初始化问题！onCreateContentView 是SVG的方式，需要找到APF中的初始化代码
    @Override
    protected void onCreateContentView(View contentView, Bundle savedInstanceState) {
        ///这里是SVG的主要初始化项目     需要添加相应的代码！！！
        initViews(contentView);
    }

    //应该返回到主界面去monitor_basic_fragment，而不是R.layout.fragment_monitor_capacitor
    @Override
    protected int getLayoutResId() {
        return R.layout.monitor_basic_fragment;
    }


    //新增串口接收监听器
    private OnSerialReceiveListener mSerialListener = new OnSerialReceiveListener() {
*//*        @Override
        public void onVoltagePhaseSequence(VoltagePhaseSequence sequence) {
            mVoltagePhaseSequenceTv.setText(sequence.msg);//电压相序  不需要
        };*//*

        @Override
        public void onRefreshMain(double active, double reactive, double factor) {
            mActiveTv.setText(CommUtil.dataDot(active, 1));
            mRectiveTv.setText(CommUtil.dataDot(reactive, 1));
            mFactorTv.setText(CommUtil.dataDot(factor, 2));
        }

 *//*       @Override
        public void onRefreshABC(List<ABCValueEntity> entitys) {
            mAbcView.setAbcData(entitys);//abc三相数据  不需要
        }*//*

        @Override
        public void onCapacitorState(CapacitorEntity entity) {
            mStateView.refreshData(entity.getListRes());//传入  获取的电容开关状态
            mStateView.refreshTvData(entity.mPassiveType);
        }

    };

    private void initViews(View view) {
        mStateView = (CapacitanceStateView) view.findViewById(R.id.monitor_state_view);
//        mVoltagePhaseSequenceTv = (TextView) view.findViewById(R.id.monitor_voltage_phase_sequence);
//        mActiveTv = (TextView) view.findViewById(R.id.monitor_active);//功率因数
//        mRectiveTv = (TextView) view.findViewById(R.id.monitor_reactive);//无功功率
//        mFactorTv = (TextView) view.findViewById(R.id.monitor_factor);//功率因数   三个参数不需要
//        mAbcView = (ABCValueView) view.findViewById(R.id.monitor_abc_view);
        SerialAPI.getInstance().registerListener(mSerialListener);
        CapacitorEntity entity = SerialAPI.getInstance().getCapacitorEntity();
        mStateView.refreshData(entity.getListRes());//获取 电容的图形资源  刷新界面
        mStateView.refreshTvData(entity.mPassiveType);//刷新  电容上的数值
//        InitAbcData();
    }*/
}
